<template>
  <div class="app-container">
    <div class="searchContianer" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}"> 
      <div class="searchLeft" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}">
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="68px">
          <el-form-item label="消息拥有者ID(订单/产品)" prop="msgSourceId" class="mb8">
            <el-input
              v-model="queryParams.msgSourceId"
              placeholder="请输入消息拥有者ID(订单/产品)"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="用户类型(1客户2师傅3运营)" prop="userType" class="mb8">
            <el-select v-model="queryParams.userType" placeholder="请选择用户类型(1客户2师傅3运营)" clearable>
              <el-option
                v-for="dict in dict.type.sk_server_client"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="目标用户" prop="userId" class="mb8">
            <el-input
              v-model="queryParams.userId"
              placeholder="请输入目标用户"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="消息标题" prop="msgTitle" class="mb8">
            <el-input
              v-model="queryParams.msgTitle"
              placeholder="请输入消息标题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="消息状态信息" prop="msgStatusDesc" class="mb8">
            <el-input
              v-model="queryParams.msgStatusDesc"
              placeholder="请输入消息状态信息"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="消息描述" prop="msgDescribe" class="mb8">
            <el-input
              v-model="queryParams.msgDescribe"
              placeholder="请输入消息描述"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="是否需要短信通知" prop="isNeedSms" class="mb8">
            <el-select v-model="queryParams.isNeedSms" placeholder="请选择是否需要短信通知" clearable>
              <el-option
                v-for="dict in dict.type.sk_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否已发短信" prop="isSendSms" class="mb8">
            <el-select v-model="queryParams.isSendSms" placeholder="请选择是否已发短信" clearable>
              <el-option
                v-for="dict in dict.type.sk_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发短信时间" prop="sendSmsTime" class="mb8">
            <el-date-picker clearable
              v-model="queryParams.sendSmsTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择发短信时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="是否需要微信消息通知" prop="isNeedWxmsg" class="mb8">
            <el-select v-model="queryParams.isNeedWxmsg" placeholder="请选择是否需要微信消息通知" clearable>
              <el-option
                v-for="dict in dict.type.sk_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否已发微信通知" prop="isSendWxmsg" class="mb8">
            <el-select v-model="queryParams.isSendWxmsg" placeholder="请选择是否已发微信通知" clearable>
              <el-option
                v-for="dict in dict.type.sk_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发微信消息通知时间" prop="sendWxmsgTime" class="mb8">
            <el-date-picker clearable
              v-model="queryParams.sendWxmsgTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择发微信消息通知时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="是否已阅读" prop="isRead" class="mb8">
            <el-select v-model="queryParams.isRead" placeholder="请选择是否已阅读" clearable>
              <el-option
                v-for="dict in dict.type.sk_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="阅读时间" prop="readTime" class="mb8">
            <el-date-picker clearable
              v-model="queryParams.readTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择阅读时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="是否显示" prop="isShow" class="mb8">
            <el-input
              v-model="queryParams.isShow"
              placeholder="请输入是否显示"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status" class="mb8">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in dict.type.sk_common_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="searchRight">
        <div style="margin-bottom:20px;margin-top:5px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>

    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vanger:message:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vanger:message:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['vanger:message:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" border :data="messageList" @selection-change="handleSelectionChange"
    	 :default-sort="defaultSort" @sort-change="handleSortChange" :height="bigTableData">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="ID" align="left" prop="id" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="消息来源类型(1订单消息2产品消息)" align="left" prop="msgSourceType" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="消息拥有者ID(订单/产品)" align="left" prop="msgSourceId" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="消息类别(1超时消息2派单消息3其他)" align="left" prop="msgKind" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="用户类型(1客户2师傅3运营)" align="left" prop="userType" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_server_client" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="目标用户" align="left" prop="userId" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="消息标题" align="left" prop="msgTitle" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="消息状态信息" align="left" prop="msgStatusDesc" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="消息描述" align="left" prop="msgDescribe" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="是否需要短信通知" align="left" prop="isNeedSms" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.isNeedSms"/>
        </template>
      </el-table-column>
      <el-table-column label="是否已发短信" align="left" prop="isSendSms" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.isSendSms"/>
        </template>
      </el-table-column>
      <el-table-column label="发短信时间" align="left" prop="sendSmsTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendSmsTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要微信消息通知" align="left" prop="isNeedWxmsg" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.isNeedWxmsg"/>
        </template>
      </el-table-column>
      <el-table-column label="是否已发微信通知" align="left" prop="isSendWxmsg" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.isSendWxmsg"/>
        </template>
      </el-table-column>
      <el-table-column label="发微信消息通知时间" align="left" prop="sendWxmsgTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendWxmsgTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已阅读" align="left" prop="isRead" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.isRead"/>
        </template>
      </el-table-column>
      <el-table-column label="阅读时间" align="left" prop="readTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.readTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="left" prop="isShow" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="状态" align="left" prop="status" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" prop="remark" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="操作" align="left" fixed="right" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view"
            @click="handleDetail(scope.row)" v-hasPermi="['vanger:message:query']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['vanger:message:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['vanger:message:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消息通知对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="85%" append-to-body :close-on-click-modal="false"
       @close="closeDialog" class="abow_dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="showDetail">
        <el-form-item label="消息拥有者ID(订单/产品)" prop="msgSourceId">
          <el-input v-model="form.msgSourceId" placeholder="请输入消息拥有者ID(订单/产品)" />
        </el-form-item>
        <el-form-item label="用户类型(1客户2师傅3运营)" prop="userType">
          <el-select v-model="form.userType" placeholder="请选择用户类型(1客户2师傅3运营)">
            <el-option
              v-for="dict in dict.type.sk_server_client"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目标用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入目标用户" />
        </el-form-item>
        <el-form-item label="消息标题" prop="msgTitle">
          <el-input v-model="form.msgTitle" placeholder="请输入消息标题" />
        </el-form-item>
        <el-form-item label="消息状态信息" prop="msgStatusDesc">
          <el-input v-model="form.msgStatusDesc" placeholder="请输入消息状态信息" />
        </el-form-item>
        <el-form-item label="消息描述" prop="msgDescribe">
          <el-input v-model="form.msgDescribe" placeholder="请输入消息描述" />
        </el-form-item>
        <el-form-item label="是否需要短信通知" prop="isNeedSms">
          <el-select v-model="form.isNeedSms" placeholder="请选择是否需要短信通知">
            <el-option
              v-for="dict in dict.type.sk_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否已发短信" prop="isSendSms">
          <el-select v-model="form.isSendSms" placeholder="请选择是否已发短信">
            <el-option
              v-for="dict in dict.type.sk_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发短信时间" prop="sendSmsTime">
          <el-date-picker clearable
            v-model="form.sendSmsTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发短信时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否需要微信消息通知" prop="isNeedWxmsg">
          <el-select v-model="form.isNeedWxmsg" placeholder="请选择是否需要微信消息通知">
            <el-option
              v-for="dict in dict.type.sk_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否已发微信通知" prop="isSendWxmsg">
          <el-select v-model="form.isSendWxmsg" placeholder="请选择是否已发微信通知">
            <el-option
              v-for="dict in dict.type.sk_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发微信消息通知时间" prop="sendWxmsgTime">
          <el-date-picker clearable
            v-model="form.sendWxmsgTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发微信消息通知时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否已阅读" prop="isRead">
          <el-select v-model="form.isRead" placeholder="请选择是否已阅读">
            <el-option
              v-for="dict in dict.type.sk_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="阅读时间" prop="readTime">
          <el-date-picker clearable
            v-model="form.readTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择阅读时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-input v-model="form.isShow" placeholder="请输入是否显示" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sk_common_status"
              :key="dict.value"
:label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!showDetail">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessage, listUnion, getMessage, getInfoUnion, delMessage, addMessage, updMessage, exportMessage } from "@/api/vanger/message";

export default {
  name: "Message",
  dicts: ['sk_common_status', 'sk_server_client', 'sk_yes_no'],
  data() {
    return {
      // 表格高度
      bigTableData:'',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 消息通知表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查看详情
      showDetail: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 查询参数
      queryParams: {
        params: {},
        pageNum: 1,
        pageSize: 10,
        msgSourceType: null,
        msgSourceId: null,
        msgKind: null,
        userType: null,
        userId: null,
        msgTitle: null,
        msgStatusDesc: null,
        msgDescribe: null,
        isNeedSms: null,
        isSendSms: null,
        sendSmsTime: null,
        isNeedWxmsg: null,
        isSendWxmsg: null,
        sendWxmsgTime: null,
        isRead: null,
        readTime: null,
        isShow: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.handleSortChange(this.defaultSort);
  },
  mounted() {
    this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
  },
  watch: {
    showSearch() {
      this.$nextTick(function () {
        this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
      })
    }
  },
  methods: {
  
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 查询消息通知列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.closeDialog()
    },
	// 关闭时设置
    closeDialog() {
      this.open = false;
      this.showDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        msgSourceType: null,
        msgSourceId: null,
        msgKind: null,
        userType: null,
        userId: null,
        msgTitle: null,
        msgStatusDesc: null,
        msgDescribe: null,
        isNeedSms: null,
        isSendSms: null,
        sendSmsTime: null,
        isNeedWxmsg: null,
        isSendWxmsg: null,
        sendWxmsgTime: null,
        isRead: null,
        readTime: null,
        isShow: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {};
      this.resetForm("queryForm");
      // this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息通知";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息通知";
      });
    },
    
    /** 查看按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看消息通知";
        this.showDetail = true;
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消息通知编号为"' + ids + '"的数据项？').then(function() {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vanger/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
